package com.wiegand.content.contents.projectcontent {
	import com.caurina.transitions.Tweener;
	import com.caurina.transitions.properties.FilterShortcuts;
	import com.wiegand.event.ClickEvent;
	import com.wiegand.gui.text.CssTextSprite;
	import com.wiegand.util.ColorRectangle;

	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.filters.BlurFilter;
	import flash.filters.GlowFilter;

	/**
	 * @author gandy
	 */
	public class ProjectItem extends Sprite {

		private var _xml : XMLList;
		private var _txt : Sprite;
		private var _bg : Sprite;
		private var _hit : Sprite;
		private var _itemWidth : int;
		private var _hSpace : int = 30;
		private var _blurFactor : int = 3;
		private var _blur : BlurFilter = new BlurFilter(_blurFactor,_blurFactor);

		private var _glowFactor : int = 5;
		private var _glow : GlowFilter = new GlowFilter(0xffffff,.5,0,0,2,3);

		public static const EVENT_ACTION : String = "click action";
		
		private var _over : Boolean = false;

		public function ProjectItem(xml:XMLList, itemWidth:int) {
			_xml = xml;
			_itemWidth = itemWidth;
			init();
		}
		
		private function init() : void {
			FilterShortcuts.init();
			name = _xml.@id;
			
			_txt = new Sprite();
			
			var title : Sprite = CssTextSprite.create(String(_xml.name), "projectscroller");
			title.x = _hSpace;
			title.width = _itemWidth;
			title.scaleY = title.scaleX;
			_txt.addChild(title);
			
			var subtitle : String;

			if( String(_xml.year) != null && String(_xml.year) != "" ) subtitle = String(_xml.year);
			
			if( String(_xml.subtitle) != null && String(_xml.subtitle) != "" ){
				if(subtitle!=null && subtitle!="") subtitle += " ||| " + String(_xml.subtitle);
				else subtitle = String(_xml.subtitle);
			}
		
			if(subtitle!=null && subtitle!=""){
				var sub : Sprite = CssTextSprite.create(subtitle, "projectscrollerSubtitle");
				sub.x = title.x;
				sub.y = title.y + title.height;
				_txt.addChild(sub);
			}
		
			_txt.alpha = .6;
			_txt.filters = [_glow,_blur];
			
			_bg = ColorRectangle.create(0xffffff,0,_txt.width+_hSpace*2,_txt.height);
			addChild(_bg);
			
			addChild(_txt);
		
			if(_xml.@hit != "false"){
				_hit = ColorRectangle.create(0xff0000,0,_txt.width+_hSpace*2,_txt.height);
				_hit.x = _txt.x;
				_hit.addEventListener(MouseEvent.CLICK, click);
				_hit.addEventListener(MouseEvent.MOUSE_OVER, over);
				_hit.addEventListener(MouseEvent.MOUSE_OUT, out);
				_hit.buttonMode=true;
				addChild(_hit);
			}
		}
		
		private function click(e:MouseEvent) : void {
			dispatchEvent( new ClickEvent(EVENT_ACTION, name) );
			Tweener.addTween(_txt,{_Glow_blurX:_glowFactor*3,_Glow_blurY:_glowFactor*3,delay:0,time:0.1,transition:"linear"});
			Tweener.addTween(_txt,{_Glow_blurX:_glowFactor,_Glow_blurY:_glowFactor,delay:0.1,time:0.1,transition:"linear"});
		}
		
		private function over(e:MouseEvent) : void {
			_over=true;
			Tweener.removeTweens(_txt);
			Tweener.addTween(_txt,{alpha:1,time:0.1,transition:"linear"});
			Tweener.addTween(_txt,{_Blur_blurX:0,_Blur_blurY:0,time:0.2,transition:"linear"});
			Tweener.addTween(_txt,{_Glow_blurX:_glowFactor,_Glow_blurY:_glowFactor,delay:0.2,time:0.2,transition:"linear"});
		}
		
		private function out(e:MouseEvent) : void {
			_over=false;
			_txt.alpha = .6;
			//???
//			Tweener.removeTweens(_txt);
//			Tweener.addTween(_txt,{alpha:0.6,time:1.0,transition:"linear"});
//			Tweener.addTween(_txt,{_Glow_blurX:0,_Glow_blurY:0,time:1.0,transition:"linear"});
//			Tweener.addTween(_txt,{_Blur_blurX:_blurFactor,_Blur_blurY:_blurFactor,delay:0.5,time:0.5,transition:"linear"});
		}

		public function get projectName():String{
			return String(_xml.name);
		}
		
		public function updateBlur(factor:Number):void{
			if(!_over){
				var blur : BlurFilter = new BlurFilter(_blurFactor+factor,_blurFactor+factor);
				_txt.filters = [_glow,blur];
				_txt.alpha = .6;
			}		
		}
		
		public function get agency():String{
			return String(_xml.agency);
		}
		
		public function get client():String{
			return String(_xml.client);
		}
		
		public function get id():String{
			return String(_xml.@id);
		}
		
	}
}
